package com.ms.service.module.zone.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.account.message.SSessionCloseNotify;
import com.ms.service.module.zone.message.CPlayerLoginZoneRequest;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.annotations.Name;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ScopeType;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.message.NetMessage;

/**
 * 接收 zone 登入相關消息的 action
 * 
 * @author TW15010
 * 
 */
@Name(scopeType = ScopeType.SESSION)
public class LoginZoneAction extends BaseMessageAction {

	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(LoginZoneAction.class);

	/**
	 * 收到 client 要求角色登入 zone
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.C_PLAYER_LOGIN_ZONE_REQUEST)
	public void recvPlayerLoginZoneRequest(CPlayerLoginZoneRequest request) {
		BasicZoneModule basicZoneModule = (BasicZoneModule) Contexts
				.getModule();

		basicZoneModule.getZone().onPlayerLoginZoneRequest(request,
				getSessionID());
	}

	/**
	 * 發送回應結果
	 * 
	 * @param result
	 * @param sessionId
	 */
	public static void sendPlayerVerifyResultError(int result, GUID sessionId) {
		if (sessionId == null)
			return;
		NetMessage msg = new NetMessage();
		msg.setSessionID(sessionId);
		msg.setMessageType(MessageTypes.S_LOGIN_ZONE_ERROR);
		msg.setMessageContent(result);
		MessageAction.sendMessage(msg);
	}
	
	@MessageType(typeCode = MessageTypes.S_SESSION_CLOSE_NOTIFY)
	public void recvSessionCloseNotify(SSessionCloseNotify notify) {
		// 轉給 zone 處理
		Object object = getModule();
		if (object instanceof BasicZoneModule) {
			BasicZoneModule basicZoneModule = (BasicZoneModule) object;
			basicZoneModule.getZone().handlePlayerDisconnectNotify(notify);
		}
	}
}
